<?php

namespace App\Http\Controllers\v1;


use App\Http\Controllers\Controller;
use App\Models\GoodsBanner;
use App\Models\Goods;
use App\Models\Order;
use App\Models\Team;
use App\Models\User;
use App\Models\Volunteer;
use Illuminate\Http\Request;

class ScoreController extends Controller
{
    public function  bannerlist()
    {
        $data = GoodsBanner::orgBanner($this->getOrdId());
        $this->setData($data);
        return $this->response();
    }

    public function goodslist(Request $request)
    {
        $result = Goods::where('auto_time','<', date('Y-m-d h:i:s'))->orWhere('g_num',0)->update(['is_display' => 0]);
        $arr = Goods::where('is_display', 1)->where('org_id', $this->getOrdId())->get();
        for ($i = 0; $i < count($arr); $i++){
            if($arr[$i]->auto_time<date('Y-m-d h:i:s')){
                $arr[$i]->is_display = 0;
                $arr[$i]->save();
            }
        }

        $pageNum = request()->input('current_page', 1);
        $pageSize = request()->input('page_size', 10);

        $nodes = Goods::where('is_display', 1)->where('org_id', $this->getOrdId());

        $nodes = $nodes->orderBy('is_recommend', 'desc')->orderBy('end_time', 'asc');

        $count = $nodes->count();

        $from = ($pageNum - 1)*$pageSize;
        $list = $nodes->take($pageSize)->skip($from)->get();
//        foreach ($list as $k => $v)
//        {
//            if(!empty($v->condition))
//            {
//                $v->condition = explode(',',$v->condition);
//            }
//        }

        $pageInfo = [
            "current_page" => intval($pageNum),
            "total_page" => ceil($count / $pageSize),
            "page_size" => intval($pageSize)
        ];

        $data['list'] = $list;
        $data['page'] = $pageInfo;
        $this->setData($data);

        return $this->response();
    }

    public function goods($id)
    {
        $userinfo = User::where('token', $this->getToken())->first();
        $nodes = Goods::where('id',$id)->where('org_id', $this->getOrdId())->first();
        $u_o_num = Order::where(['user_id' => $userinfo->id, 'goods_id' => $nodes->id])->get()->count();
        
        $u_num = $nodes->u_num;
        if($u_num <= $u_o_num && !empty($nodes->u_num)){
            $change_num = 0;
        } else{
            $change_num = 1;
        }

        if(empty($nodes))
        {
            $this->setStatus(10001);
            $this->setMsg('此商品不存在');
            return $this->response();
        }

        if($nodes->is_display == 0)
        {
            $this->setStatus(10006);
            $this->setMsg('此商品已下架');
            return $this->response();
        }

        $team = Team::find($nodes->team_id);
        $nodes->team_id = $team;


//        if(!empty($nodes->condition))
//        {
//            $nodes->condition = explode(',',$nodes->condition);
//        }
//        return $nodes;

        $this->setData([
            'g_name' => $nodes->g_name,
            'team_info' => $nodes->team_id,
            'org_id' => $this->getOrdId(),
            'sponsor' => $nodes->sponsor,
            'g_img' => $nodes->g_img,
            'content' => $nodes->content,
            'type' => $nodes->type,
            'price' => $nodes->price,
            'points' => $nodes->points,
            'g_num' => $nodes->g_num,
            'change_num' => $change_num,
            'created_at' => $nodes->start_time,
            'updated_at' => $nodes->end_time,
            'condition' => $nodes->condition
        ]);
        return $this->response();
    }

    public function change(Request $request,$id)
    {
        $userinfo = User::where('token', $this->getToken())->first();
        $score = Volunteer::where('user_id', $userinfo->id)->first();
        $nodes = Goods::where('id',$id)->where('org_id', $this->getOrdId())->first();

        if(empty($nodes))
        {
            $this->setStatus(10001);
            $this->setMsg('此商品不存在');
            return $this->response();
        }

        if($nodes->is_display == 0)
        {
            $this->setStatus(10006);
            $this->setMsg('此商品已下架');
            return $this->response();
        }

        if($nodes->g_num === 0)
        {
            $this->setStatus(10007);
            $this->setMsg('此商品已售罄');
            return $this->response();
        }

        $user_score = $score->score;
        $points = $nodes->points;

        if($user_score < $points){
            $this->setStatus(10002);
            $this->setMsg('您的星币不足');
            return $this->response();
        }

        $order_num = time().substr($userinfo->phone,-4);

        $order = new Order();

        $order->fill(['order_num' => $order_num,
            'team_id' => $nodes->team_id,
            'org_id' => $this->getOrdId(),
            'goods_id' => $id,
            'user_id' => $userinfo->id,
            'status' => $nodes->examine==1?2:0,
            'num' => 1,
            'points' => $nodes->points,
            'state' => 0
        ]);

        if($order->save())
        {
            $score->score = $user_score - $points;
            $score->save();

            if(!empty($nodes->g_num)){
                $nodes->g_num = $nodes->g_num - 1;
                $nodes->save();
            }

            if($nodes->examine == 1){
                $this->setData([
                    'msg' => 0,
                    'points' => $nodes->points,
                    'time' => date('Y-m-d h:i:s')
                ]);
            } else {
                $this->setData([
                    'msg' => 1,
                    'points' => $nodes->points,
                    'time' => date('Y-m-d h:i:s'),
                    'access' => $nodes->access
                ]);
            }
            return $this->response();
        }

    }

    public function orders()
    {
        $userinfo = User::where('token', $this->getToken())->first();

        $pageNum = request()->input('current_page', 1);
        $pageSize = request()->input('page_size', 10);

        $nodes = Order::with(['goods_id','user_id',]);
        $nodes = $nodes->where('user_id', $userinfo->id)->where('org_id', $this->getOrdId());

        $count = $nodes->count();

        $from = ($pageNum - 1)*$pageSize;
        $list = $nodes->take($pageSize)->skip($from)->get();
        $pageInfo = [
            "current_page" => intval($pageNum),
            "total_page" => ceil($count / $pageSize),
            "page_size" => intval($pageSize)
        ];

        $data['list'] = $list;
        $data['page'] = $pageInfo;
        $this->setData($data);

        return $this->response();

    }

    public function collect($id)
    {
        $userinfo = User::where('token', $this->getToken())->first();
        $nodes = Order::find($id);
        if(empty($nodes))
        {
            $this->setStatus(10003);
            $this->setMsg('未找到此订单');
            return $this->response();
        }

        if($userinfo->id != $nodes->user_id)
        {
            $this->setStatus(10004);
            $this->setMsg('请确认是否是自己的订单');
            return $this->response();
        }

        if($nodes->status==0)
        {
            $nodes->state = 1;
            $nodes->collect_time = Date("Y-m-d h:i:s",time());
            if($nodes->save())
            {
                return $this->response();
            }
        } else {
            $this->setStatus(10005);
            $this->setMsg('还未审核');
            return $this->response();
        }
    }
}